package sd.czw.com.mmgapp;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;

import sd.czw.com.mmgapp.activity.LoginActivity;
import sd.czw.com.mmgapp.customviews.NoScrollViewPager;
import sd.czw.com.mmgapp.fragment.CategeroyFragment;
import sd.czw.com.mmgapp.fragment.PersonalFragment;
import sd.czw.com.mmgapp.fragment.ShoppingCartFragment;
import sd.czw.com.mmgapp.fragment.ShouyeFragment;
import sd.czw.com.mmgapp.utils.SpUtils;

public class MieMieGouApp extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private RadioGroup radioGroup;
    private float width;
   // private String token = null;
    private RadioButton rb_sy,rb_fl,rb_cart,rb_person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initviews();
    }

    private void initviews() {
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(this);

        rb_sy = (RadioButton) findViewById(R.id.rb_shouye);
        rb_cart = (RadioButton) findViewById(R.id.rb_cart);
        rb_fl = (RadioButton) findViewById(R.id.rb_fenl);
        rb_person = (RadioButton) findViewById(R.id.rb_person);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //关闭预加载，默认一次只加载一个Fragment
        viewPager.setOffscreenPageLimit(3);
        //添加Fragment
        fragmentList.add(new ShouyeFragment());
        fragmentList.add(new CategeroyFragment());
        fragmentList.add(new ShoppingCartFragment());
        fragmentList.add(new PersonalFragment());
        //适配器
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(0,false);
        //viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.rb_shouye);
                        break;
                    case 1:
                        radioGroup.check(R.id.rb_fenl);
                        break;
                    case 2:
                        radioGroup.check(R.id.rb_cart);
                        break;
                    case 3:
                        radioGroup.check(R.id.rb_person);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        Drawable maindra = getResources().getDrawable(R.drawable.home_icon);
        Drawable fenleidra = getResources().getDrawable(R.drawable.cate_icon);
        Drawable discoverdra = getResources().getDrawable(R.drawable.cart_icon);
        Drawable persionaldra = getResources().getDrawable(R.drawable.mine_icon);
        Drawable maindra1 = getResources().getDrawable(R.drawable.home_icon_selected);
        Drawable fenleidra1 = getResources().getDrawable(R.drawable.cate_icon_selected);
        Drawable discoverdra1 = getResources().getDrawable(R.drawable.cart_icon_selected);
        Drawable persionaldra1 = getResources().getDrawable(R.drawable.mine_icon_selected);


        maindra.setBounds(0, 0, maindra.getMinimumWidth(), maindra.getMinimumHeight());
        fenleidra.setBounds(0, 0, fenleidra.getMinimumWidth(), fenleidra.getMinimumHeight());
        discoverdra.setBounds(0, 0, discoverdra.getMinimumWidth(), discoverdra.getMinimumHeight());
        persionaldra.setBounds(0, 0, persionaldra.getMinimumWidth(), persionaldra.getMinimumHeight());
        maindra1.setBounds(0, 0, maindra1.getMinimumWidth(), maindra1.getMinimumHeight());
        fenleidra1.setBounds(0, 0, fenleidra1.getMinimumWidth(), fenleidra1.getMinimumHeight());
        discoverdra1.setBounds(0, 0, discoverdra1.getMinimumWidth(), discoverdra1.getMinimumHeight());
        persionaldra1.setBounds(0, 0, persionaldra1.getMinimumWidth(), persionaldra1.getMinimumHeight());

        switch (i){
            case R.id.rb_shouye:
                viewPager.setCurrentItem(0,false);
                rb_sy.setCompoundDrawables(null, maindra1, null, null);
                rb_fl.setCompoundDrawables(null, fenleidra, null, null);
                rb_cart.setCompoundDrawables(null, discoverdra, null, null);
                rb_person.setCompoundDrawables(null, persionaldra, null, null);
                rb_sy.setTextColor(getResources().getColor(R.color.titles));
                rb_fl.setTextColor(getResources().getColor(R.color.title_text));
                rb_cart.setTextColor(getResources().getColor(R.color.title_text));
                rb_person.setTextColor(getResources().getColor(R.color.title_text));
                break;
            case R.id.rb_fenl:
                viewPager.setCurrentItem(1,false);
                rb_sy.setCompoundDrawables(null, maindra, null, null);
                rb_fl.setCompoundDrawables(null, fenleidra1, null, null);
                rb_cart.setCompoundDrawables(null, discoverdra, null, null);
                rb_person.setCompoundDrawables(null, persionaldra, null, null);
                rb_sy.setTextColor(getResources().getColor(R.color.title_text));
                rb_fl.setTextColor(getResources().getColor(R.color.titles));
                rb_cart.setTextColor(getResources().getColor(R.color.title_text));
                rb_person.setTextColor(getResources().getColor(R.color.title_text));
                break;
            case R.id.rb_cart:
                viewPager.setCurrentItem(2,false);
                rb_sy.setCompoundDrawables(null, maindra, null, null);
                rb_fl.setCompoundDrawables(null, fenleidra, null, null);
                rb_cart.setCompoundDrawables(null, discoverdra1, null, null);
                rb_person.setCompoundDrawables(null, persionaldra, null, null);
                rb_sy.setTextColor(getResources().getColor(R.color.title_text));
                rb_fl.setTextColor(getResources().getColor(R.color.title_text));
                rb_cart.setTextColor(getResources().getColor(R.color.titles));
                rb_person.setTextColor(getResources().getColor(R.color.title_text));
                break;
            case R.id.rb_person:
                String token = SpUtils.setToken(getApplicationContext()).getString("token", "");
                System.out.println("token="+token);
                if (token != null && !("".equals(token))){
                        viewPager.setCurrentItem(3,false);
                        rb_sy.setCompoundDrawables(null, maindra, null, null);
                        rb_fl.setCompoundDrawables(null, fenleidra, null, null);
                        rb_cart.setCompoundDrawables(null, discoverdra, null, null);
                        rb_person.setCompoundDrawables(null, persionaldra1, null, null);
                        rb_sy.setTextColor(getResources().getColor(R.color.title_text));
                        rb_fl.setTextColor(getResources().getColor(R.color.title_text));
                        rb_cart.setTextColor(getResources().getColor(R.color.title_text));
                        rb_person.setTextColor(getResources().getColor(R.color.titles));
                    }else {
                        Intent intent = new Intent(MieMieGouApp.this, LoginActivity.class);
                        intent.putExtra("flag_activity","person");
                        startActivity(intent);
                        rb_person.setChecked(false);
                    }
//                }
//        else {
//                    Intent intent = new Intent(MieMieGouApp.this, LoginActivity.class);
//                    intent.putExtra("flag_activity","person");
//                    startActivity(intent);
//                    rb_person.setChecked(false);
//                }
                break;
        }
    }


    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
